package edu.zut.cs.software.perfect.process.web.spring.controller;

import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;

import edu.zut.cs.software.perfect.base.web.spring.controller.GenericController;
import edu.zut.cs.software.perfect.process.domain.CourseProcess;
import edu.zut.cs.software.perfect.process.service.CourseProcessManager;

/**
 * @ClassName CourseProcessController
 * @Description TODO
 * @Author niu-xi
 * @Date 2020年5月30日 上午12:24:18
 * @Version 1.0
 */
@ResponseBody
@Controller
@RequestMapping("/courseProcess")
public class CourseProcessController extends GenericController<CourseProcess,Long,CourseProcessManager>{

	
	CourseProcessManager  courseProcessManager;
	
	@Autowired
	public void setCourseProcessManager(CourseProcessManager courseProcessManager) {
		this.courseProcessManager = courseProcessManager;
		this.manager = courseProcessManager;
	}
	
	@ResponseBody
    @GetMapping(value = "findall", produces = "application/json;charset=utf-8")
	public String findAllCourseInformation() {
		List<CourseProcess> courseProcessList = this.manager.findAll();
        @SuppressWarnings({ "rawtypes", "unchecked" })
		PageInfo pageInfo = new PageInfo(courseProcessList);
        return JSON.toJSONString(pageInfo);
	}
	
	@GetMapping("/findbyid/{id}")
	public String findById(@PathVariable("id") Long id) {
		Optional<CourseProcess> result = this.manager.findByCourseId(id);
		return JSON.toJSONString(result);
	}
	
//	@PostMapping("/save")
	@GetMapping("/save/{name}")
//	public CourseProcess save(@RequestBody CourseProcess courseProcess) {
	public CourseProcess save(@PathVariable("name") String courseName) {
		CourseProcess courseProcess= new CourseProcess();
		courseProcess.setCourseName(courseName);
		CourseProcess result = courseProcessManager.save(courseProcess);
		return result ;
	}
	
//	@DeleteMapping("/deletebyid/{id}")
	@GetMapping("/deletebyid/{id}")
	public Boolean deleteById(@PathVariable("id") Long id) {
//		Optional<CourseProcess> courseProcess = this.manager.findByCourseId(id);
		Boolean result = this.manager.deleteById(id);
		return result;
	}
	
	@GetMapping("/updata/{id}")
//	public CourseProcess updata(@RequestBody CourseProcess courseProcess) {
	public CourseProcess updata(@PathVariable("id") Long id) {
		Optional<CourseProcess> courseProcess = this.manager.findByCourseId(id);
		CourseProcess courseProcess_s = courseProcess.get();
		courseProcess_s.setCourseName("wuli");
		CourseProcess result = this.manager.save(courseProcess_s);
		return result;
	}

	
}
